package aitsm;

import com.toedter.calendar.JDateChooser;
import java.sql.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class ProcessPublic {
    private String day;
    private String month;
    private String year;
    protected ProcessPublic(){        
    }
    public Vector getConlumnName(ResultSet rs){
        Vector kq = new Vector();
        try{
            for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++){
                kq.add(rs.getMetaData().getColumnName(i));
            }
        }catch(Exception ex){
            System.out.println(ex);
        }
        return kq;
    }
    public Vector getData(ResultSet rs){
        Vector kq = new Vector();
        try{
            if(rs != null){
                while(rs.next()){
                    for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++){
                        kq.add(rs.getString(i));
                    }
                }
            }
        }catch(Exception ex){
            System.out.println(ex);
        }
        return kq;
    }
    public void JTableCheckBoxRecordSet(JTable jtable, ResultSet rs){
        DefaultTableModel dtm = new DefaultTableModel();
        int col_num = dtm.getColumnCount();
        JCheckBoxRenderer jr = new JCheckBoxRenderer();
        final JCheckBox choose = new JCheckBox();        
        try{
            int numCol = rs.getMetaData().getColumnCount();            
            for(int i = 0; i < numCol; i++){
                dtm.addColumn(rs.getMetaData().getColumnName(i + 1));
            }
            dtm.addColumn("Selection"); 
            Vector v_temp;
            if(rs != null){
               while(rs.next()){
                    v_temp = new Vector();
                    for(int i = 1; i <= numCol; i++){
                        v_temp.add(rs.getString(rs.getMetaData().getColumnName(col_num - (col_num - i))));                      
                    }                    
                    dtm.addRow(v_temp);
               }
            }
            jtable.setModel(dtm);
            jtable.getColumn("Selection").setCellEditor(new DefaultCellEditor(choose));
            jtable.getColumn("Selection").setCellRenderer(jr);              
        }catch(Exception ex){
            System.out.print(ex);
        }
    }
    public void JCompoBoxRecordSet(JComboBox jb, ResultSet rs, int colNum){       
        try{
            if(rs != null){                
                while(rs.next()){                    
                   jb.addItem(rs.getString(colNum));                   
                }                
            }
        }catch(Exception ex){
            System.out.print(ex);
        }     
    }
    public boolean checkDataTypeTextField(JTextField jtfName, String dataType){
        boolean kq = true;
        switch(dataType){
            case "String":
                String tempS = jtfName.getText().trim().toLowerCase();
                if(tempS.equals("")){
                    JOptionPane.showMessageDialog(null, "Value isn't empty");
                    kq = false;
                }else {
                    for(int i = 0; i < tempS.length(); i++){
                        if(!(tempS.charAt(i) >= 'a' && tempS.charAt(i) <= 'z')){
                            JOptionPane.showMessageDialog(null, "Character normal only");
                            kq = false;
                            break;
                        }
                    }
                }
                break;
            case "Integer":
                String tempI= jtfName.getText().trim().toLowerCase();
                if(tempI.equals("")){
                    JOptionPane.showMessageDialog(null, "Value isn't empty");
                    kq = false;
                }else {
                    for(int i = 0; i < tempI.length(); i++){
                        if(!(tempI.charAt(i) >= '0' && tempI.charAt(i) <= '9')){
                            JOptionPane.showMessageDialog(null, "Not digits");
                            kq = false;
                            break;
                        }
                    }
                }
                break;
            case "Double":
                String tempDo = jtfName.getText().trim().toLowerCase();
                try{
                    Double.parseDouble(tempDo);                    
                }catch(Exception ex){
                    JOptionPane.showMessageDialog(null, "Data must to double");
                }            
            default:
                break;
        }
        return kq;
    }
    public boolean checkDataTypeDateChooser(JDateChooser jdc){
        boolean kq = true;
        if(jdc.getDate() == null){
            kq = false;
            JOptionPane.showMessageDialog(null, "Values to format: MM/dd/yyyy");
        }            
        return kq;
    }
    public String getStringInsertData(String tableName){
        String kq = "";
        Connect_class cc = new Connect_class();
        ResultSet temp = cc.getRecordSet("Select * from " + tableName);
        Vector nameColumn = getConlumnName(temp);
        StringBuilder strInsert = new StringBuilder("insert into ");
        strInsert.append(tableName);
        strInsert.append("(");
        for(Object s : nameColumn){
            strInsert.append(s);
            strInsert.append(",");
        }
        strInsert.deleteCharAt(strInsert.length() - 1);
        strInsert.append(")");
        strInsert.append(" values(");
        for(int i = 0; i < nameColumn.size(); i++){
            strInsert.append("?");
            strInsert.append(",");
        }
        strInsert.deleteCharAt(strInsert.length() - 1);
        strInsert.append(")");
        kq = strInsert.toString();
//        System.out.println(kq);

        return kq;
    }
    public String getStringUpdateData(String tableName, int numColumn, String value){
        String kq = "";
        Connect_class cc = new Connect_class();
        ResultSet temp = cc.getRecordSet("Select * from " + tableName);
        Vector nameColumn = getConlumnName(temp);
        StringBuilder tempUpdate = new StringBuilder("update ");
        tempUpdate.append(tableName);
        tempUpdate.append(" set ");
        for(Object s: nameColumn){
            tempUpdate.append(s);
            tempUpdate.append(" = ?, ");
        }
        tempUpdate.deleteCharAt(tempUpdate.length() - 2);
        tempUpdate.append("where ");        
        tempUpdate.append(nameColumn.elementAt(numColumn).toString());
        tempUpdate.append(" like '");
        tempUpdate.append(value);
        tempUpdate.append("'");
        System.out.println(tempUpdate.toString());
        return null;
    }    
    public String insertData(String StringInsertData, Vector vectorData, Connection con){
        try {            
            PreparedStatement sta = con.prepareStatement(StringInsertData);
            int i = 1;
            for(Object temp : vectorData){
                sta.setString(i, temp.toString());
                //System.out.println(temp + " " + i);
                i++;
            }             
            int row = sta.executeUpdate();
                if(row > 0)
                    return "Inserted successfully";
                else
                    return "Inserted ERROR";  
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }
        return null;
    }
    public String updateData(String StringUpdateData, Vector vectorData, Connection con){
        try{
            PreparedStatement sta = con.prepareStatement(StringUpdateData);
            int i = 1;
            for(Object s: vectorData){
                sta.setString(i, s.toString());
                i++;
            }
            int row = sta.executeUpdate();
            if(row > 0)
                return "Update successfully";
            else
                return "Update ERROR";
        }catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex);
        }
        return null;
    }
    public String covertDate(JDateChooser jdc){
        String temp = jdc.getDate().toString();
        String day = "";
        String month = "";        
        String year = "";
        String kq = "";
        StringBuilder stemp = new StringBuilder(temp);
            for(int i = 0; i < stemp.length(); i++){
                if(stemp.charAt(i) == ' '){
                    stemp.delete(0, i + 1);
                    break;
                }
            }
            for(int i = 0; i < stemp.length(); i++){
                if(stemp.charAt(i) == ' '){
                    for(int j = 0; j < i; j++){
                        month += stemp.charAt(j);
                    }
                    stemp.delete(0, i + 1);
                    break;
                }
            }
            day += stemp.charAt(0);
            day += stemp.charAt(1);
            stemp.delete(0, stemp.length() - 4);
            year = stemp.toString();
            String[] Tmonth = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
            for(int i = 0; i < Tmonth.length; i++){
                if(month.equals(Tmonth[i])){
                    month = Integer.toString(i + 1);                   
                }
            }
        kq = month + "/" + day + "/" + year;
        return kq;
    }
//  a Bang
        public String getDay(){
        return this.day;
    }
    public String getMonth(){
        return this.month;
    }
    public String getYear(){
        return this.year;
    }
    public boolean checkInteger(JTextField jtfName){
        boolean kq = true;
        String tempI= jtfName.getText().trim().toLowerCase();
        for(int i = 0; i < tempI.length(); i++){
            if(!(tempI.charAt(i) >= '0' && tempI.charAt(i) <= '9')){                            
                kq = false;
                break;
            }
        }
        return kq;
    }
    public void updateOnJTable(String tableName, int colTableDatabase, String valueCondition, Connection con, Vector dataUpdate){        
        Connect_class cc = new Connect_class();
        ResultSet temp = cc.getRecordSet("Select * from " + tableName);
        Vector nameColumn = getConlumnName(temp);
        StringBuilder tempUpdate = new StringBuilder("update ");
        tempUpdate.append(tableName);
        tempUpdate.append(" set ");
        for(Object s: nameColumn){
            tempUpdate.append(s);
            tempUpdate.append(" = ?, ");
        }
        tempUpdate.deleteCharAt(tempUpdate.length() - 2);
        tempUpdate.append("where ");        
        tempUpdate.append(nameColumn.elementAt(colTableDatabase).toString());
        tempUpdate.append(" like '");
        tempUpdate.append(valueCondition);
        tempUpdate.append("'");
        System.out.println(tempUpdate.toString());
        try{
            PreparedStatement sta = con.prepareStatement(tempUpdate.toString());
            //int i = 0;
            for(int i = 1; i <= dataUpdate.size(); i++){
                System.out.print("Phan tu thu " + i + ": "+ dataUpdate.elementAt(i-1).toString() + " : ");
                sta.setString(i, dataUpdate.elementAt(i-1).toString());
            }
            int row = sta.executeUpdate();
            if(row > 0)
                System.out.println("Update successfully");
            else
                System.out.println("Update ERROR");
        }catch(Exception ex){
            System.out.println(ex);
        }
    }
    public void JTableRecordSet(JTable jtable, ResultSet rs){
        DefaultTableModel dtm = new DefaultTableModel();
        int col_num = dtm.getColumnCount();
        JCheckBoxRenderer jr = new JCheckBoxRenderer();
        final JCheckBox choose = new JCheckBox();        
        try{
            int numCol = rs.getMetaData().getColumnCount();            
            for(int i = 0; i < numCol; i++){
                dtm.addColumn(rs.getMetaData().getColumnName(i + 1));
            }            
            Vector v_temp;
            if(rs != null){
               while(rs.next()){
                    v_temp = new Vector();
                    for(int i = 1; i <= numCol; i++){
                        if(rs.getMetaData().getColumnName(i).toLowerCase().equals("gender")){                            
                            if(rs.getString(i).equals("0")){
                                v_temp.add("Female");
                            }else{
                                v_temp.add("Male");
                            }
                        }else{
                            v_temp.add(rs.getString(rs.getMetaData().getColumnName(col_num - (col_num - i))));
                        }                                              
                    }                    
                    dtm.addRow(v_temp);
               }
            }
            jtable.setModel(dtm);                       
        }catch(Exception ex){
            System.out.print(ex);
        }
    }
//    end a Bang
    //Minh add
    public void ChangeName(JTable table, int col_index, String col_name){
        table.getColumnModel().getColumn(col_index).setHeaderValue(col_name);
    }
//end Minh
    
}